package api.apiservice.apiopenplat.Utils.verify;

import api.apiservice.apiopenplat.Utils.HttpUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.regex.Pattern;

public class verifyApier extends verifyAction{

    private  String apiUri ;

    private String requestMethod;

    private String responseBody;

    private String appfront;

    public verifyApier(String appfront,String apiUri,String requestMethod){
        this.apiUri = apiUri;
        this.requestMethod = requestMethod;
        this.appfront = appfront;

    }

    @Override
    public boolean verifyApiUri() {
        String[] split = apiUri.split("/");
        String pattern = "^[A-Za-z0-9]+$";
        boolean isCorrectPattern = true;
        for (int i = 1 ; i < split.length;i++){
            isCorrectPattern = Pattern.matches(pattern, split[i]);
        }
        return isCorrectPattern;
    }

    @Override
    public boolean verifyApiInvoke() {
        boolean uriPass = false;
        if (appfront != null && apiUri != null){
            appfront = appfront + apiUri;
            String responseCode = HttpUtils.requestByGet(appfront);
            if (responseCode.equals("20000")) uriPass = true;
        }
        return  uriPass;
    }

    @Override
    public boolean verifyApiResponse() {
        return true;
    }
}
